<!DOCTYPE html>
<html>
    <?php
    include_once '../DB/db.php';
    include_once '../Functions/cookieFunctions.php';
    include_once '../Functions/imageFunctions.php';
    ?>

    <head>
        <title>Nettbutikk</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        
        <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
    </head>
    <body>
        <div class="wrapper">
            <?php
            include_once "../MainFrame/header.php";
            include_once "../MainFrame/menuBar.php";
            include_once "../MainFrame/sideBar.php";
            ?>


            <div class="content">
                <h1>Handlekurv</h1>


                <?php
                if (isset($_COOKIE['vare'])) { //Henter cookien med varer hvis de finnes
                    $arr = returnValues('vare'); //Spør etter en array av cookien

                    /*------------Overskriftene til tabellene under------------*/
                    
                    
                    echo '<table id="searchTable" >';
                    echo '<tr><th></th><th></th><th></th><th></th>' . '<th width="60px">Antall: </th>' . '<th width="100">Pris: </th>' . '<th>Totalt: </th></tr>';
                    $totPris = 0;   //Summen i handlevogna
                    /*-----------Går igjennom alle varene i cookien----------*/
                    for ($i = 0; $i < sizeof($arr); $i+=2) {

                        $id = $arr[$i]; //Id'n ligger først
                        $antall = $arr[$i + 1]; //Antall ligger en etter

                        $items = $db->query("
                            SELECT *
                            FROM item 
                              WHERE vid = " . $id
                        );

                        /*--------Verdiene man trenger fra database-------------*/
                        $row = $items->fetch();
                        if ($row) {
                            $item_name = $row['name'];
                            $item_vareid = $row['vid'];
                            $item_price = $row['price'];
                            $item_lager = $row['stock'];
                            $item_image = $row['picture'];
                            $item_image = getImageFromBlob($item_image, 100 , 80);

                            if ($item_lager > 0) {
                                $item_lager = "På lager!";
                            } else {
                                $item_lager = "Ikke på lager!";
                            }
                        }
                        /* --------Hver enkelt vare som listes opp i handlekurven:----------- */
                        echo '<form id="oppdater" name="oppdater" method="get" action="/Functions/cookieFunctions.php" > ';
                            echo '<tr>';
                                echo '<td width="200px">' . $item_image . '</td>';
                                echo '<td width="230px">Vare navn: ' . $item_name . '<br />' . "Lagerstatus: " . $item_lager . '<br /> Vare ID: ' . $item_vareid . '</td>';
                                echo '<td width="52px"> <input type="submit" name="Slett" value="Slett"> </td>';
                                echo '<td width="92px"> <input type="submit" name="Oppdater" value="Oppdater"> </td>';
                                echo '<input type="hidden" name="vareID" value="' . $item_vareid . '">';
                                echo '<td width="72px"><input style="text-align: right;" type = "text" name="antall" value="' . $antall . '" placeholder="$antal" size="2"> <br /> </td>';
                                echo '<td width="90px">' . $item_price . ',-</td>';
                                echo '<td width="100px">' . $item_price * $antall . ',-</td>';
                                $totPris += ( $item_price * $antall); //Totalprisen i handlekurven
                            echo '</tr>';
                        echo '</form>';
                    }
                    echo '</table>';

                    /*----------------Valg for handlekurv------------------*/
                    echo '<table>';
                    echo '<form id="oppdater" name="oppdater" method="get" action="/Functions/cookieFunctions.php" > ';
                        echo '<tr>';
                            echo '<td width="650px"> <input type="submit" name="Tøm" value="Tøm handlekurv"></td>';
                            echo '<td>Total pris: ' . $totPris . ',-</td>';
                        echo  '</tr>';
                    echo '</form>';
                    echo ' ';
                        echo '<tr>';
                            echo '<td>';
                                echo '<form id="oppdater" name="oppdater" method="post" action="/index.php" >';
                                    echo '<input type="submit" name="Fortsett" value="Fortsett å handle">';
                                echo '</form>';
                            echo '</td>';
                            echo '<td>';
                                echo '<form id="oppdater" name="oppdater" method="post" action="/Views/kassa.php" >';
                                    echo '<input type="submit" name="Kassa" value="Gå til kassa">';
                                echo '</form>';
                            echo '</td>';
                        echo '</tr>';
                    echo '</table>';
                } else {
                    echo '<h3 style="color:white; margin-left:100px">Det finnes ingen varer i handlekurven!</h3>';

                }
                ?>
            </div>

            <?php
                include_once "../MainFrame/footer.php";
            ?>
        </div>
    </body>
</html>